package com.example.apple.ddhr_new.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.activity.App;
import com.api.param.LoginParam;
import com.api.viewModel.LoginVM;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.utils.ActivityUtil;
import com.example.lzbase.BaseActivity;
import com.example.lzbase.BaseViewModelActivity;
import com.gyf.barlibrary.ImmersionBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SplashActivity extends BaseViewModelActivity<LoginVM> {


    @BindView(R.id.btn_next)
    TextView btnNext;

    @Override
    protected int titleText() {
        return 0;
    }
    private CountDownTimer timer = new CountDownTimer(3000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            btnNext.setText(millisUntilFinished / 1000 + "跳过");

        }

        @Override
        public void onFinish() {
           nextClick();

        }
    };
    @Override
    protected void created(@Nullable Bundle savedInstanceState) {

        super.created(savedInstanceState);

        timer.start();

        viewModel.user.observe(this,res->{


            res.setPwd(App.getInstance().getAccount().getPwd());
            App.getInstance().setAccount(res);
            ActivityUtil.startMainActivity(SplashActivity.this);


        });
//        new Handler().postDelayed(new Runnable() {
//            public void run() {
//
//
//                if(null==AppConstant.getInstance().getAccount()){
//                    ActivityUtil.startLoginActivity(SplashActivity.this);
//                }else {
//                    ActivityUtil.startMainActivity(SplashActivity.this);
//                }
//
//
//            }
//        }, 2000);

    }

    @Override
    public void onError(Throwable error) {
        super.onError(error);
        ActivityUtil.startLoginActivity(SplashActivity.this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public int statusColor() {
        return R.color.color_transparent;
    }

    @Override
    public void initStatusBar() {
        ImmersionBar.with(this)
                .transparentStatusBar()
                .init();


    }


    @OnClick(R.id.btn_jinru)
    public void onClick() {

        timer.cancel();
        if(null== App.getInstance().getAccount()){
            ActivityUtil.startLoginActivity(SplashActivity.this);
        }else {

            toMain();

        }

    }

    private void toMain() {

        addSubscription(viewModel.login1(new LoginParam(App.getInstance().getAccount().getUsername(),App.getInstance().getAccount().getPwd())));


    }


    @OnClick(R.id.btn_next)
    public void  nextClick() {
        timer.cancel();
        if(null== App.getInstance().getAccount()){
            ActivityUtil.startLoginActivity(SplashActivity.this);
        }else {
            toMain();
        }
    }
}
